package online.takemehand.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.math.BigDecimal;

public class VBoxHBoxExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建HBox布局，用于放置输入框和按钮
        HBox inputBox = new HBox();
        Label label = new Label("员工号:");
        TextField textField = new TextField("输入内容");
        Button button = new Button("提交");
        button.addEventHandler(ActionEvent.ACTION, e -> {
            String text = textField.getText();
            System.out.println(text);
        });

        inputBox.getChildren().addAll(label,textField, button);
        inputBox.setSpacing(10); // 设置组件间间距为10像素
        inputBox.setAlignment(Pos.CENTER_LEFT); // 设置整体对齐方式为居中左对齐
        // 创建TableView布局，用于展示数据
        TableView<Person> tableView = new TableView<>();
        TableColumn<Person, String> nameColumn = new TableColumn<>("姓名");
        TableColumn<Person, Integer> ageColumn = new TableColumn<>("年龄");
        TableColumn<Person, Integer> amountColumn = new TableColumn<>("金额");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
        amountColumn.setCellValueFactory(new PropertyValueFactory<>("amount"));
        tableView.getColumns().addAll(nameColumn, ageColumn,amountColumn);
        tableView.getItems().addAll(
                new Person("Alice", 25, BigDecimal.valueOf(10.003)),
                new Person("Bob", 30,BigDecimal.valueOf(10.003))
        );
        tableView.setMaxWidth(500);
        tableView.setMaxHeight(300);


        TableView<Person> tableView2 = new TableView<>();
        TableColumn<Person, String> nameColumn2 = new TableColumn<>("姓名");
        TableColumn<Person, Integer> ageColumn2 = new TableColumn<>("年龄");
        TableColumn<Person, Integer> amountColumn2 = new TableColumn<>("金额");
        nameColumn2.setCellValueFactory(new PropertyValueFactory<>("name"));
        ageColumn2.setCellValueFactory(new PropertyValueFactory<>("age"));
        amountColumn2.setCellValueFactory(new PropertyValueFactory<>("amount"));
        tableView2.getColumns().addAll(nameColumn2, ageColumn2,amountColumn2);
        tableView2.getItems().addAll(
                new Person("Alice", 25, BigDecimal.valueOf(10.003)),
                new Person("Bob", 30,BigDecimal.valueOf(10.003))
        );
        tableView2.setMaxWidth(500);
        tableView2.setMaxHeight(300);
        HBox hBox = new HBox();
        hBox.setSpacing(10); // 设置组件间间距为10像素
        hBox.setAlignment(Pos.TOP_LEFT); // 设置整体对齐方式为居中左对齐
        hBox.getChildren().addAll(tableView,tableView2);
        VBox.setVgrow(hBox, Priority.ALWAYS);

        // 创建VBox布局，将输入框和表格组合在一起
        VBox root = new VBox(inputBox, hBox);
//        VBox.setMargin(inputBox, new javafx.geometry.Insets(10));
//        VBox.setMargin(hBox, new javafx.geometry.Insets(10));
        root.setSpacing(10);
        root.setAlignment(Pos.CENTER_LEFT); // 设置整体对齐方式为居中左对齐
        // 设置场景并展示窗口
        primaryStage.setScene(new Scene(root, 400, 300));
        primaryStage.setTitle("HBox + VBox布局示例");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    // 数据模型类
    public static class Person {
        private String name;
        private int age;
        private BigDecimal amount;

        public Person(String name, int age, BigDecimal amount) {
            this.name = name;
            this.age = age;
            this.amount = amount;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public BigDecimal getAmount() {
            return amount;
        }
    }
}